describe("不指定初始化参数", function(){
	var client = new HttpClient();
	
	it('baseUrl为空', function(){
		expect(client.baseUrl()).toEqual('');
	});
	
	it('debug 标志是 false', function(){
		expect(client.debug()).toEqual(false);
	});
});

describe("指定初始化参数", function(){
	var baseUrl = 'base/url';
	var timeoutCode = 1;
	
	var client = new HttpClient({
		baseUrl:baseUrl
	});
	
	it('baseUrl不为空', function(){
		expect(client.baseUrl()).toEqual(baseUrl);
	});
	
	describe("本地模式运行ajax", function(){
		it('加载默认变量名称', function(){
			var callback = {
				success: function(data){
					expect(data.code).toEqual(0);
				}
			};
			
			spyOn(callback, 'success');
			
			client.ajax({
				url: 'mock/node',
				success: callback.success
			});
			
			expect(callback.success).toHaveBeenCalled();
		});		
		
		it('加载get标准变量名称', function(){
			var callback = {
				success: function(data){
					expect(data.code).toEqual(0);
				}
			};
			
			spyOn(callback, 'success');
			
			client.ajax({
				url: 'mock/element',
				success: callback.success
			});
			
			expect(callback.success).toHaveBeenCalled();
		});		
		
		it('加载post标准变量名称', function(){
			var callback = {
				success: function(data){
					expect(data.code).toEqual(0);
				}
			};
			
			spyOn(callback, 'success');
			
			client.ajax({
				url: 'mock/element',
				type: 'post',
				success: callback.success
			});
			
			expect(callback.success).toHaveBeenCalled();
		});		
		
		it('调用结束处理', function(){
			var callback = {
				success: function(data){
					expect(data.code).toEqual(0);
				},
				afterRequest: function(data){
					expect(data.code).toEqual(0);
				}
			};
			
			spyOn(callback, 'success');
			spyOn(callback, 'afterRequest');
			
			client.ajax({
				url: 'mock/element',
				afterRequest: callback.afterRequest
			});
			
			expect(callback.success.calls.count()).toEqual(0);
			expect(callback.afterRequest).toHaveBeenCalled();
		});
		
		it('无入参的调用结束处理', function(){
			var callback = {
				success: function(data){
					expect(data.code).toEqual(0);
				},
				afterRequest: function(){
					expect(true).toEaual(true);
				}
			};
			
			spyOn(callback, 'success');
			spyOn(callback, 'afterRequest');
			
			client.ajax({
				url: 'mock/element',
				success: callback.success,
				afterRequest: callback.afterRequest
			});
			
			expect(callback.success).toHaveBeenCalled();
			expect(callback.afterRequest).toHaveBeenCalled();
		});
	});

	describe("ajax错误码处理", function(){
		it('处理登录超时错误', function(){
			var sessionTimeoutAction = {
				code: 1,
				callback: function(data){
					expect(data.code).toEqual(1);
				}
			};
			
			var hc = new HttpClient({
				baseUrl:baseUrl,
				errorCodeActions: [
					sessionTimeoutAction
				]
			});
	
			spyOn(sessionTimeoutAction, 'callback');
			
			hc.ajax({
				url: 'session/timeout',
				success: function(data){
					expect(data.code).toEqual(1);
				},
				afterRequest: function(data){
					expect(data.code).toEqual(1);
				}
			});
			
			expect(sessionTimeoutAction.callback).toHaveBeenCalled();
		});
		
		it('禁用登录超时错误处理', function(){
			var sessionTimeoutAction = {
				code: 1,
				callback: function(data){
					expect(data.code).toEqual(1);
				}
			};
			
			var hc = new HttpClient({
				baseUrl:baseUrl,
				errorCodeActions: [
					sessionTimeoutAction
				],
				supressCodes: [1]
			});
	
			spyOn(sessionTimeoutAction, 'callback');
			
			hc.ajax({
				url: 'session/timeout',
				success: function(data){
					expect(data.code).toEqual(1);
				}
			});
			
			expect(sessionTimeoutAction.callback.calls.count()).toEqual(0);
		});
	});
	
});
